Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

56 9kyo hwang #204

Merged
merged 7 commits into from
Sep 2, 2024
Merged

56 9kyo hwang #204

merged 7 commits into from
Sep 2, 2024

Conversation

9kyo-hwang
Copy link
Collaborator

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

15681 ํŠธ๋ฆฌ์™€ ์ฟผ๋ฆฌ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

40๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

1. ๋ฌธ์ œ

๊ฐ„์„ ์— ๊ฐ€์ค‘์น˜์™€ ๋ฐฉํ–ฅ์„ฑ์ด ์—†๋Š” ์ž„์˜์˜ ๋ฃจํŠธ ์žˆ๋Š” ํŠธ๋ฆฌ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‹ค์Œ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ.

  • ์ •์  U๋ฅผ ๋ฃจํŠธ๋กœ ํ•˜๋Š” ์„œ๋ธŒํŠธ๋ฆฌ์— ์†ํ•œ ์ •์ ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

์ œํ•œ ์‚ฌํ•ญ

  • ํŠธ๋ฆฌ ์ •์ ์˜ ์ˆ˜ $2 \le N \le 10^5$
  • ๋ฃจํŠธ ๋ฒˆํ˜ธ $1 \le R \le N$
  • ์ฟผ๋ฆฌ ์ˆ˜ $1 \le Q \le 10^5$

2. ์„ค๋ช…

์ฟผ๋ฆฌ ์ˆ˜๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ๋งค ์ฟผ๋ฆฌ๋งˆ๋‹ค ์„œ๋ธŒ ํŠธ๋ฆฌ์— ์†ํ•œ ์ •์  ์ˆ˜๋ฅผ ๊ตฌํ•  ์ˆ˜๋Š” ์—†๋‹ค.
๊ฒฐ๊ตญ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ์บ์‹ฑํ•ด๋†”์•ผ ํ•˜๋Š”๋ฐ, ์ด๋Ÿฐ ์‹์˜ ์•„์ด๋””์–ด๊ฐ€ ์ ์šฉ๋œ ๋ฌธ์ œ๋ฅผ ๋ณดํ†ต "ํŠธ๋ฆฌ DP"๋ž€ ์œ ํ˜•์œผ๋กœ ๋ฌถ์ธ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” DFS ์ˆœํšŒ์ด๋‹ค. ๊ตณ์ด ํŠธ๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๋”๋ผ๋„, ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ๋งŒ ํ•ด์ค€๋‹ค๋ฉด ํŠธ๋ฆฌ๋ฅผ ์ˆœํšŒํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ํšจ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
์ฆ‰ ๋ฃจํŠธ์—์„œ DFS ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด, ๋ฆฌํ”„ ๋…ธ๋“œ๊นŒ์ง€ ๋‚ด๋ ค๊ฐ”๋‹ค๊ฐ€ ์˜ฌ๋ผ์˜ค๋ฉด์„œ ๋ถ€๋ชจ ๋…ธ๋“œ๋“ค์˜ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ˜•ํƒœ๋ฅผ ๋„๊ฒŒ ๋œ๋‹ค.

๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์ž…๋ ฅ์€ ๋ฃจํŠธ๊ฐ€ 5๋ฒˆ์ด๊ณ , ์—ฃ์ง€ ์ •๋ณด๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1 3
4 3
5 4
5 6
6 7
2 3
9 6
6 8

์ด ์ •๋ณด๋ฅผ ํ†ตํ•ด ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌ์„ฑํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

node[1] - 
node[2] -
node[3] - [1] - [2]
node[4] - [3]
node[5] - [4] - [6]
node[6] - [5] - [7] - [9] - [8]
node[7] -
node[8] -
node[9] -

์ด์ œ 5๋ฒˆ ๋…ธ๋“œ๋ถ€ํ„ฐ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•ด ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋ฉฐ ๊ฐ’์„ ๊ตฌํ•œ ๋’ค, ๊ทธ ๊ฐ’์„ dp ํ…Œ์ด๋ธ”์— ์ €์žฅํ•˜๋ฉด ๋œ๋‹ค.

  • ์˜ˆ๋ฅผ ๋“ค์–ด, node[6]์—๋Š” 5, 7, 8, 9๋ฒˆ์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€๋งŒ ๋ฃจํŠธ์ธ 5๋ฒˆ์€ ์ด๋ฏธ visited ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์ง€ ์•Š๋Š”๋‹ค.
  • ์ด๋Ÿฌํ•œ ์›๋ฆฌ๋กœ 5 -> 4 -> 3 -> 1 ๊นŒ์ง€ ์žฌ๊ท€์ ์œผ๋กœ ๋“ค์–ด๊ฐ”๋‹ค๊ฐ€, ๋Œ์•„๋‚˜์˜ค๋ฉด์„œ dp[3] += dp[1] ์ด ์ˆ˜ํ–‰๋˜๊ณ  2๋ฒˆ ๋…ธ๋“œ๋กœ ๊ฐ„ ๋’ค, ๋‹ค์‹œ ๋Œ์•„๋‚˜์˜ค๋ฉด์„œ dp[3] += dp[2]๊ฐ€ ์ˆ˜ํ–‰๋œ๋‹ค. ๋”ฐ๋ผ์„œ dp[3]์—๋Š” 3์ด ์ €์žฅ๋œ๋‹ค.

์ œ๋ชฉ์„-์ž…๋ ฅํ•ด์ฃผ์„ธ์š”_

int N, R, Q; cin >> N >> R >> Q;
vector<vector<int>> Tree(N + 1);
for(int i = 0; i < N - 1; ++i)
{
    int U, V; cin >> U >> V;
    Tree[U].emplace_back(V);
    Tree[V].emplace_back(U);
}

vector<int> Size(N + 1, 1);
vector<bool> Visited(N + 1, false);

์•ž์„œ์„œ ์–˜๊ธฐํ•œ ๋Œ€๋กœ "์ธ์ ‘๋ฆฌ์ŠคํŠธ" ํ˜•์‹์œผ๋กœ ํŠธ๋ฆฌ๋ฅผ ํ‘œํ˜„ํ•œ๋‹ค.
dp ํ…Œ์ด๋ธ”๊ณผ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋ฆฌ์ŠคํŠธ๋„ ๊ฐ๊ฐ ์„ ์–ธํ•œ๋‹ค.

function<int(int)> DFS = [&](int Parent)
{
    if(Visited[Parent])
    {
        return Size[Parent];
    }
    
    Visited[Parent] = true;
    for(const int Child : Tree[Parent])
    {
        if(!Visited[Child])
        {
            Size[Parent] += DFS(Child);
        }
    }
    
    return Size[Parent];
}; DFS(R);

์ตœ์ดˆ์˜ DFS ํ•จ์ˆ˜๋Š” ๋ฃจํŠธ ๋…ธ๋“œ์—์„œ ์‹œ์ž‘ํ•œ๋‹ค.
ํ˜„์žฌ ๋…ธ๋“œ Parent์—์„œ Visited๋ฅผ ์ฒดํฌํ•œ ๋’ค, Child ๋…ธ๋“œ๋“ค์„ ํ™•์ธํ•˜๋ฉฐ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด DFS ํ•จ์ˆ˜๋ฅผ ์žฌ๊ท€ ํ˜ธ์ถœํ•ด ๊ทธ ๊ฒฐ๊ณผ๋ฅผ dp ํ…Œ์ด๋ธ”์— ์ €์žฅํ•œ๋‹ค.
๋งŒ์•ฝ ํ˜„์žฌ Parent ๋…ธ๋“œ๊ฐ€ ์ด์ „์— ๋ฐฉ๋ฌธํ–ˆ์—ˆ๋”๋ผ๋ฉด ๋ถ€๋ชจ -> ์ž์‹ -> ๋ถ€๋ชจ๋กœ ๋‹ค์‹œ ํ™•์ธํ•˜๋Š” ๊ณผ์ •, ์ฆ‰ ๋ฆฌํ”„ ๋…ธ๋“œ์— ๋„๋‹ฌํ–ˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ DP ํ…Œ์ด๋ธ”์— ๋‹ด๊ธด ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

while(Q--)
{
    int U; cin >> U;
    cout << Size[U] << "\n";
}

๋ฃจํŠธ ๋…ธ๋“œ์— ๋Œ€ํ•ด DFS๋ฅผ ํ˜ธ์ถœํ–ˆ๋‹ค๋ฉด dp ํ…Œ์ด๋ธ”์— ๊ฐ ๋…ธ๋“œ ๋ณ„ ์„œ๋ธŒ ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ ๊ฐœ์ˆ˜๊ฐ€ ๋ชจ๋‘ ์ €์žฅ๋ผ์žˆ์œผ๋ฏ€๋กœ, ์ฟผ๋ฆฌ์— ๋Œ€ํ•ด์„œ๋Š” ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋Š” ๊ฐ’์„ ๋ฐ”๋กœ ์ถœ๋ ฅ์ฃผ๋ฉด ๋œ๋‹ค.

3. ์ „์ฒด ์ฝ”๋“œ

#include <iostream>
#include <vector>
#include <functional>

using namespace std;

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    
    int N, R, Q; cin >> N >> R >> Q;
    vector<vector<int>> Tree(N + 1);
    for(int i = 0; i < N - 1; ++i)
    {
        int U, V; cin >> U >> V;
        Tree[U].emplace_back(V);
        Tree[V].emplace_back(U);
    }
    
    vector<int> Size(N + 1, 1);
    vector<bool> Visited(N + 1, false);
    
    function<int(int)> DFS = [&](int Parent)
    {
        if(Visited[Parent])
        {
            return Size[Parent];
        }
        
        Visited[Parent] = true;
        for(const int Child : Tree[Parent])
        {
            if(!Visited[Child])
            {
                Size[Parent] += DFS(Child);
            }
        }
        
        return Size[Parent];
    }; DFS(R);

    while(Q--)
    {
        int U; cin >> U;
        cout << Size[U] << "\n";
    }

    return 0;
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

ํŠธ๋ฆฌ DP ๋ฌธ์ œ ์ค‘์—” ์‰ฌ์šด ๋ฌธ์ œ๋ผ ํฌ๊ฒŒ ์–ด๋ ค์›€ ์—†์ด ํ‘ผ ๊ฒƒ ๊ฐ™๋‹ค.
๋‹ค์Œ์—” ์ข€ ๋” ์–ด๋ ค์šด ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค.

Copy link
Collaborator

@mjj111 mjj111 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ฒ˜์Œ์— ๊ฐ ์ฟผ๋ฆฌ์— ๋Œ€ํ•ด ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ํ•ด์„œ ํ”„๋ฆฐํŠธํ•˜๋ฉด ๋˜์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ–ˆ๋”๋‹ˆ
O(n**2) ๋ผ์„œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ๋ชจ์ƒ‰ํ•ด์•ผ๋งŒ ํ—€๋„ค์˜

์ด์ „ ๊ฒฝ๋กœ ํƒ์ƒ‰์— ๋ฉ”๋ชจ์ œ์ด์…˜์„ ์ž…ํžˆ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ €๋„ DP ์ ์šฉํ•ด์„œ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค!

import java.io.*;
import java.util.*;

public class Main {

	static int[] children;
	static List<Integer>[] nodes;

	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		int n = Integer.parseInt(st.nextToken());
		int r = Integer.parseInt(st.nextToken());
		int q = Integer.parseInt(st.nextToken());
		
		nodes = new ArrayList[n+1];
		for(int i=1; i<n+1; i++) {
			nodes[i] = new ArrayList<>();
		}

		children = new int[n+1];
		Arrays.fill(children, 1);
        
		for(int i=0; i<n-1; i++) {
			st = new StringTokenizer(br.readLine());
			int u = Integer.parseInt(st.nextToken());
			int v = Integer.parseInt(st.nextToken());
			
			nodes[u].add(v);
			nodes[v].add(u);
		}
		
		traversal(r, -1);
		
		for(int i=0; i<q; i++) {
			int idx = Integer.parseInt(br.readLine());
			System.out.println(children[idx]);
		}
	}
	
	static void traversal(int idx, int pa) {
		for(int next : nodes[idx]) {
			if(pa != next) {
				traversal(next, idx);
			}
		}
		
		if(pa!=-1) {
			children[pa] += children[idx];
		}
	}
}

@9kyo-hwang 9kyo-hwang merged commit 0945df5 into main Sep 2, 2024
4 checks passed
@9kyo-hwang 9kyo-hwang deleted the 56-9kyo-hwang branch September 2, 2024 13:56
Copy link
Member

@xxubin04 xxubin04 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์™œ ๋”์ด์ƒ ์ž์‹๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด, dp[n] += dp[n-1]์„ ํ•˜๋Š”๊ฐ€ ํ–ˆ๋Š”๋ฐ, ๊ทธ๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ ๋…ธ๋“œ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ ํšŸ์ˆ˜๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๊ฑฐ์˜€๋„ค์š”.
์ด๋Ÿฐ ์•„์ด๋””์–ด๋Š” ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•ด๋‚ด์‹œ๋Š” ๊ฑด์ง€...๐Ÿง
ํŠธ๋ฆฌ ์ˆœํšŒ ๋ฌธ์ œ๊ฐ€ ๋‚˜์˜จ๋‹ค๋ฉด ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ๊ณ ๋ฏผํ•ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค~!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants